using System;
using System.Text.RegularExpressions;
using System.Xml;

namespace FreeFlow.Client
{
	/// <summary>
	/// A text field.
	/// </summary>
	[Serializable]
	public class TextField : Field
	{
    internal TextField(Form form, XmlElement fieldNode)
      : base(form, fieldNode)
		{
      if (fieldNode != null)
      {
        align = ParseAlign(fieldNode.Attributes["Align"].Value);
        if (fieldNode.Attributes["MaxLen"] != null)
          maxLength = int.Parse(fieldNode.Attributes["MaxLen"].Value);
        if (fieldNode.Attributes["Secret"] != null)
          password = fieldNode.Attributes["Secret"].Value == "1";
        if (fieldNode["Pattern"] != null)
          mask = fieldNode["Pattern"].InnerText;
      }
		}

    private string unencodedValue;
    /// <summary>
    /// Gets or sets the value of the field.
    /// </summary>
    public override string Value
    {
      get { return base.Value; }
      set 
      { 
        unencodedValue = value;
        if (Password)
          base.Value = Connection.MD5Encode(value);
        else
          base.Value = value; 
      }
    }

    // ensure we don't MD5 encode when reading in responses from engine
    internal override void FillValue(XmlElement node)
    {
      XmlElement valNode = node["Value"];
      if (valNode != null)
        base.Value = valNode.InnerText;
    }

    /// <summary>
    /// Gets the validation message.
    /// </summary>
    /// <returns>Returns null if the field has no validation errors, an error message otherwise.</returns>
    public override string GetValidationMessage()
    {
      if (Usage == FieldUsage.Required)
      {
        // handle password fields     
        if (Password)
          if (Value == Connection.MD5Encode(""))
            return FieldMustBeCompleted;
      }

      if (unencodedValue != null)
      {
        if (unencodedValue.Length > MaxLength)
          return "Maximum allowed length for this field is " + MaxLength.ToString() + " characters";
      }

      // mask
      if ((Mask != null) && (Mask.Length > 0))
      {
        Regex regex = new Regex(Mask);
        if (!regex.Match(unencodedValue).Success)
          return "Field does not match the mask '" + Mask + "'";
      }

      return base.GetValidationMessage();
    }

    #region properties
    private Align align;
    /// <summary>
    /// Gets the alignment of the text field.
    /// </summary>
    public Align Align
    {
      get { return align; }
    }

    private int maxLength = 250;
    /// <summary>
    /// Gets the maximum length allowed in the text field.
    /// </summary>
    public int MaxLength
    {
      get { return maxLength; }
    }

    private bool password;
    /// <summary>
    /// Gets a value indicating whether this <see cref="TextField"/> is a password field.
    /// </summary>
    /// <value>
    /// 	<c>true</c> if a password field; otherwise, <c>false</c>.
    /// </value>
    public bool Password
    {
      get {return password; }
    }

    private string mask;
    /// <summary>
    /// Gets the regular expression mask for the <see cref="TextField"/>.
    /// </summary>
    public string Mask
    {
      get
      {
        return mask;
      }
    }

    #endregion
	}
}
